package com.zhixue.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixue.content.mapper.CourseTeacherMapper;
import com.zhixue.content.model.dto.CourseTeacherDto;
import com.zhixue.content.model.po.CourseTeacher;
import com.zhixue.content.service.CourseTeacherService;
import com.zhixue.exception.ZhixuePlusException;
import com.zhixue.r.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/4/11
 * @Description: com.zhixue.content.service.impl
 * @version: 1.0
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper teacherMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseTeacher addTea(CourseTeacherDto teacherDto) {
        // todo 教师头像后续完成
        if (teacherDto == null) {
            throw new ZhixuePlusException("参数为null");
        }
        //字段参数校验跳过
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(teacherDto, courseTeacher);
        teacherMapper.insert(courseTeacher);
        return teacherMapper.selectById(courseTeacher.getId());
    }

    @Override
    public List<CourseTeacher> selectByCourseId(Long id) {
        //todo 教师头像后续完成
        if (id == null) {
            throw new ZhixuePlusException("参数为null");
        }
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId, id);
        return teacherMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseTeacher updateTea(CourseTeacher courseTeacher) {
        if (courseTeacher == null) {
            throw new ZhixuePlusException("参数为null");
        }
        //字段参数校验略过
        teacherMapper.updateById(courseTeacher);
        return teacherMapper.selectById(courseTeacher.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteTea(Long courseId, Long teaId) {
        if (courseId == null || teaId == null) {
            throw new ZhixuePlusException("参数为null");
        }
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getId, teaId).eq(CourseTeacher::getCourseId, courseId);
        int delete = teacherMapper.delete(wrapper);
        return delete > 0 ? R.ok() : R.fail();
    }
}
